  DEFINE VARIABLE site     AS CHARACTER.
  DEFINE VARIABLE s_date   AS DATE.
  DEFINE VARIABLE e_date   AS DATE.
  DEFINE VARIABLE sonbr    AS CHARACTER.
  DEFINE VARIABLE soship   AS CHARACTER.
  DEFINE VARIABLE part     AS CHARACTER.
  DEFINE VARIABLE cust     AS CHARACTER.
  DEFINE VARIABLE io_type  AS CHARACTER.
  DEFINE VARIABLE sumfor   AS CHARACTER.

  DEFINE VARIABLE prod_qty_tot LIKE tr_qty_loc.
  DEFINE VARIABLE prod_amt_tot LIKE tr_gl_amt.
  DEFINE VARIABLE prod_cst_tot LIKE sct_cst_tot.
  DEFINE VARIABLE cust_qty_tot LIKE tr_qty_loc.
  DEFINE VARIABLE cust_amt_tot LIKE tr_gl_amt.
  DEFINE VARIABLE cust_cst_tot LIKE sct_cst_tot.

  DEFINE VARIABLE um_conv AS DECIMAL INITIAL 1.
  DEFINE VARIABLE tax_pct AS DECIMAL INITIAL 0.17.
  DEFINE VARIABLE cst_tot AS DECIMAL INITIAL 0.
  DEFINE VARIABLE tax_amt AS DECIMAL INITIAL 0.
  DEFINE VARIABLE tax_amt2 AS DECIMAL INITIAL 0.

  site = "210".
  s_date = TODAY - 90.
  e_date = TODAY.
  sonbr = "".
  soship = "".
  part = "P15-6350004".
  cust = "".
  io_type = "RCT-TR".
  sumfor = "".

  IF sumfor = "PART" THEN DO:
  
      FOR EACH tr_hist WHERE INDEX(io_type, tr_type) > 0
                         AND (io_type = "" OR INDEX("ORD-SO,ORD-PO,CST-ADJ",tr_type) = 0)
                         AND (s_date <= tr_effdate OR s_date = ?)
                         AND (tr_effdate <= e_date OR e_date = ?)
                         AND tr_nbr BEGINS sonbr 
                         AND tr_part BEGINS part
                         AND tr_ship_id BEGINS soship 
                         AND tr_addr BEGINS cust 
                         AND (tr_site = site OR site = "")
                         NO-LOCK BREAK BY tr_part :
    
             FIND FIRST sod_det WHERE sod_nbr = tr_nbr AND sod_line = tr_line NO-LOCK NO-ERROR.
             IF AVAILABLE sod_det THEN um_conv = sod_um_conv.
    
             IF FIRST-OF (tr_part) THEN DO:
                prod_qty_tot = 0.
                prod_amt_tot = 0.
                prod_cst_tot = 0.
                tax_pct      = 0.17.
             END.
             
             prod_qty_tot = prod_qty_tot + tr_qty_loc * um_conv.
             prod_amt_tot = prod_amt_tot + tr_qty_loc * um_conv * tr_price.
             prod_cst_tot = prod_cst_tot + tr_gl_amt.
            
    /*          DISP tr_site tr_addr tr_qty_loc tr_price WITH WIDTH 200. */
    
             IF LAST-OF(tr_part) THEN DO:
    .        
                FIND FIRST pt_mstr WHERE pt_part = tr_part NO-LOCK NO-ERROR.
                FOR EACH ptp_det WHERE ptp_part = tr_part AND (ptp_pm_code = "M" OR ptp_pm_code = "P") NO-LOCK,
                    EACH sct_det WHERE sct_site = ptp_site AND sct_part = ptp_part AND sct_sim = "Standard" NO-LOCK:
                     tax_pct = ((sct_mtl_ll + sct_lbr_ll + sct_sub_ll + sct_bdn_ll + sct_ovh_ll) * ptp__dec01 + 
                                (sct_mtl_tl + sct_lbr_tl + sct_sub_tl + sct_bdn_tl + sct_ovh_ll) * ptp__dec02) / sct_cst_tot.
                     cst_tot = sct_cst_tot.
 DISP ptp_part tax_pct sct_cst_tot ptp__dec02 sct_mtl_ll sct_lbr_ll sct_sub_ll sct_bdn_ll    
                                    ptp__dec01 sct_mtl_tl sct_lbr_tl sct_sub_tl sct_bdn_tl . 

                END.
    
                DISP
                       
                       tr_part
                       pt_um
                       prod_qty_tot  
                       prod_amt_tot
                       prod_cst_tot
                       cst_tot
                       tax_pct 
                       pt_part_type
                       pt_desc1 + pt_desc2 WHEN (AVAILABLE pt_mstr)
                       
                WITH WIDTH 200 NO-ERROR.
             END.   
         
      END.

  END.
  ELSE IF sumfor = "CUST" THEN DO:

      FOR EACH tr_hist WHERE INDEX(io_type, tr_type) > 0
                         AND (io_type = "" OR INDEX("ORD-SO,ORD-PO,CST-ADJ",tr_type) = 0)
                         AND (s_date <= tr_effdate OR s_date = ?)
                         AND (tr_effdate <= e_date OR e_date = ?)
                         AND tr_nbr BEGINS sonbr 
                         AND tr_part BEGINS part
                         AND tr_ship_id BEGINS soship 
                         AND tr_addr BEGINS cust 
                         AND (tr_site = site OR site = "")
                         NO-LOCK BREAK BY tr_addr BY tr_part :
    
             FIND FIRST sod_det WHERE sod_nbr = tr_nbr AND sod_line = tr_line NO-LOCK NO-ERROR.
             IF AVAILABLE sod_det THEN um_conv = sod_um_conv.
    
             IF FIRST-OF (tr_part) THEN DO:
                prod_qty_tot = 0.
                prod_amt_tot = 0.
                prod_cst_tot = 0.
             END.
             
             prod_qty_tot = prod_qty_tot + tr_qty_loc * um_conv.
             prod_amt_tot = prod_amt_tot + tr_qty_loc * um_conv * tr_price.
             prod_cst_tot = prod_cst_tot + tr_gl_amt.
            
/* DISP tr_site tr_addr tr_qty_loc tr_price WITH WIDTH 200. */
    
             IF LAST-OF(tr_part) THEN DO:
    .        
                FIND FIRST pt_mstr WHERE pt_part = tr_part NO-LOCK NO-ERROR.
                FOR EACH ptp_det WHERE ptp_part = tr_part AND (ptp_pm_code = "M" OR ptp_pm_code = "P") NO-LOCK,
                    EACH sct_det WHERE sct_site = ptp_site AND sct_part = ptp_part AND sct_sim = "Standard" NO-LOCK:
                     tax_pct = ((sct_mtl_ll + sct_lbr_ll + sct_sub_ll + sct_bdn_ll + sct_ovh_ll) * ptp__dec01 + 
                                (sct_mtl_tl + sct_lbr_tl + sct_sub_tl + sct_bdn_tl + sct_ovh_ll) * ptp__dec02) / sct_cst_tot.
                     cst_tot = sct_cst_tot.
/* DISP ptp_part tax_pct sct_cst_tot ptp__dec02 sct_mtl_ll sct_lbr_ll sct_sub_ll sct_bdn_ll   */
/*                                   ptp__dec01 sct_mtl_tl sct_lbr_tl sct_sub_tl sct_bdn_tl . */
                END.
    
                DISP
                       tr_addr
                       tr_part
                       pt_um
                       prod_qty_tot  
                       prod_amt_tot
                       prod_cst_tot
                       cst_tot
                       tax_pct 
                       pt_part_type
                       pt_desc1 + pt_desc2 WHEN (AVAILABLE pt_mstr)
                       
                WITH WIDTH 200 NO-ERROR.
             END.   
         
      END.

  END.
  ELSE DO:

      FOR EACH tr_hist WHERE INDEX(io_type, tr_type) > 0
                         AND (io_type = "" OR INDEX("ORD-SO,ORD-PO,CST-ADJ",tr_type) = 0)
                         AND (s_date <= tr_effdate OR s_date = ?)
                         AND (tr_effdate <= e_date OR e_date = ?)
                         AND tr_nbr BEGINS sonbr 
                         AND tr_part BEGINS part
                         AND tr_ship_id BEGINS soship 
                         AND tr_addr BEGINS cust 
                         AND (tr_site = site OR site = "")
                         NO-LOCK BREAK BY tr_ship_id :
    
             FIND FIRST sod_det WHERE sod_nbr = tr_nbr AND sod_line = tr_line NO-LOCK NO-ERROR.
             IF AVAILABLE sod_det THEN um_conv = sod_um_conv.
    
             prod_qty_tot = tr_qty_loc * um_conv.
             prod_amt_tot = tr_qty_loc * um_conv * tr_price.
             prod_cst_tot = tr_gl_amt.
            
             FIND FIRST pt_mstr WHERE pt_part = tr_part NO-LOCK NO-ERROR.
             FOR EACH ptp_det WHERE ptp_part = tr_part AND (ptp_pm_code = "M" OR ptp_pm_code = "P") NO-LOCK,
                 EACH sct_det WHERE sct_site = ptp_site AND sct_part = ptp_part AND sct_sim = "Standard" NO-LOCK:
                     tax_pct = ((sct_mtl_ll + sct_lbr_ll + sct_sub_ll + sct_bdn_ll + sct_ovh_ll) * ptp__dec01 + 
                                (sct_mtl_tl + sct_lbr_tl + sct_sub_tl + sct_bdn_tl + sct_ovh_ll) * ptp__dec02) / sct_cst_tot.
                     IF tax_pct = ? OR tax_pct = 0 THEN tax_pct = 0.17.
             END.
             tax_amt = tr_gl_amt * tax_pct. 
             tax_amt2 = IF (tr_price > 0 AND tr_price - abs(tr_gl_amt * ( 1 + tax_pct)/ tr_qty_loc) > 0) THEN 
                           (((- tr_qty_loc * um_conv * tr_price - tr_gl_amt * ( 1 + tax_pct)) / 1.17) * 0.1768) 
                        ELSE 0.

/* MESSAGE " price=" tr_price "gl_amt=" tr_gl_amt "qty=" tr_qty_loc "tax=" tax_amt2. */

             
             DISP tr_site   
                  tr_nbr
                  tr_ship_id
                  tr_part
                  pt_um
                  tr_addr
                  prod_qty_tot  
                  prod_amt_tot
                  prod_cst_tot
                  tax_pct 
                  tax_amt 
                  tax_amt2
                  pt_desc1 + pt_desc2 WHEN (AVAILABLE pt_mstr)
             WITH WIDTH 200 NO-ERROR.
          
      END.

  END.
